home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11031 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.5 KB  |  90 lines

  1. Path: apccorp.apcc.com!root
  2. From: nfegan@apcc.com (Noel Fegan)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Major problem with strings.
  5. Date: Tue, 12 Mar 1996 10:44:44 GMT
  6. Organization: American Power Conversion
  7. Message-ID: <4i3kkg$bar@apccorp.apcc.com>
  8. References: <31438275.72DB@aol2.com>
  9. NNTP-Posting-Host: hewie.galway.apcc.com
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. Neil <neil@aol2.com> wrote:
  13.  
  14. >Here's my code:
  15.  
  16. >1    char *club="";
  17. >2    club="/public_html/neil";
  18.  
  19. >3    strcat(club,argv[1]+5);
  20.  
  21. >4    strcat(club,"/");
  22.  
  23. >----------------------------------------
  24. >(lines are numbered for reference only)
  25.  
  26. >the argv[1] looks like: "test=12345"
  27.  
  28. >So, ultimately, I want club to look like: "/public_html/neil12345"
  29.  
  30. You have made a fundamental mistake here.
  31.  
  32. Line 1 sets the pointer "club" to point to an empty string. On the next line you
  33. change the value of "club" to point to a different string, making the first line
  34. redundant. Neither of these are wrong, but it indicates to me that you do not
  35. fully understand what is happening when you have a line like char * club="". The
  36. pointer club is set to the value of a memory address which contains a '\0'
  37. character. The variable is not set to be a '\0' character. club could be a value
  38. like 0x4DD3, a memory address which happen to contain '\0' character.
  39.  
  40. On line 2 the value of club is changed to point somewhere else in memory. This
  41. time the memory address pointed to contains a '/' character and the memory
  42. address 1 byte after this contains a 'p' character and so on until we have a
  43. string "/public_html/neil/". The final '/' character is followed by a '\0'
  44. character which terminates the string.
  45.  
  46. Line 3 is where you make your first mistake. The memory address pointed to by
  47. club after line 2 is a block of static memory which is automatically allocated
  48. by the program. The block of memory is exactly 17 bytes long (including the '\0'
  49. character). When you do a strcat you are adding to the end of the block pointed
  50. by club, which in this case is a block of memory which is not large enough to
  51. hold any more data. The strcat function sets the '\0' character at the end of
  52. the block to the '1' character. It then proceeds to write '2', '3' ... in memory
  53. space which it has no right access at all. So the "2345\0" characters are
  54. actaully written in memory In which it is not allowed to write.
  55.  
  56. Line 4 just adds to the mistake by writing 1 more byte into unowned memory. The
  57. consequence of writing in unowned territory depends on where the memory happens
  58. to be, the operating system, and other such things, suffice it to say that the
  59. results are unpredictable.
  60.  
  61. What you should do is the following:
  62.  
  63. //...
  64. char * pStub = "/public_html/neil";
  65. char * pExtra = (char *) argv[1]+5;
  66. char * pNewChars = new char[strlen(pStub) + strlen(pExtra)+1];
  67.  
  68. strcpy(pNewChars, pStub);
  69. strcat(pNewChars, pExtra);
  70. //...
  71.  
  72. The line "char * pNewChars = new char[strlen(pStub) + strlen(pExtra)+1];"
  73. allocates a buffer which is just big enough for the stub with the extra adds to
  74. it and also allows for the '\0' at the end of the string.
  75.  
  76. The code assumes that the string passed in to the program, pointed to by argv[1]
  77. has at least 5 characters in the string. The program should check for correct
  78. parameter before blindly assuming that the user typed in the correct data. If
  79. the string pointed to by argv[1] happens to be shorter than 5 characters long
  80. (not including the '\0' character) then this program could have a serious bug.
  81.  
  82.  
  83. --
  84. Noel Fegan
  85. European Software Development Department
  86. American Power Conversion
  87. I don't speak for APC...
  88. nfegan@apcc.com
  89.  
  90.